//
//  JCWelcomeController.swift
//  weibo07777
//
//  Created by mac on 15/12/17.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit
import SnapKit
class JCWelcomeController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        prepareUI()
        // TODO:数据库
        self.setupIconView()
        
        JCUserAccount.loadAccount()?.loadUserInfo({ (error) -> () in
            
            print(error)
            
            self.setupIconView()
            
        })
        
    }
    
    private func setupIconView(){
        
        print(JCUserAccount.loadAccount())
        if let avatar_large = JCUserAccount.loadAccount()?.avatar_large {
            let url = NSURL(string: avatar_large)!
//            print(JCUserAccount.loadAccount())
            
            
            // 显示当前用户头像
            self.iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
        }

    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        iconView.snp_remakeConstraints { (make) -> Void in
            make.top.equalTo(self.view).offset(100)
            make.centerX.equalTo(self.view)
            make.width.height.equalTo(160)
        }
        // 更新约束
        view.updateConstraintsIfNeeded()
        
        UIView.animateWithDuration(1.0, delay: 0.1, usingSpringWithDamping: 0.6, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.view.layoutIfNeeded()
            }, completion:{(_) -> Void in
                AppDelegate.switchRootViewController(true)
        })
        
    }
    
    override func updateViewConstraints() {

        bgImgView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(EdgeInsetsMake(0, left: 0, bottom: 0, right: 0))
        }
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.view)
            make.bottom.equalTo(self.view.snp_bottom).offset(-160)
            make.width.height.equalTo(160)
        }
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(10)
        }
        
        super.updateViewConstraints()
    }
    
    private func prepareUI(){
        view.addSubview(bgImgView)
        view.addSubview(iconView)
        view.addSubview(welcomeLabel)
        
    }
    private func loadUserInfo(){
        
      
        
        
    }
    // MARK : 约束
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        view.setNeedsUpdateConstraints()
    }
    
    
    // MARK : lazy
    private lazy var bgImgView = UIImageView(image: UIImage(named: "ad_background"))
    
    private lazy var iconView = UIImageView(image: UIImage(named: "avatar_default_big"))
    
    private lazy var welcomeLabel: UILabel = {
        
        var label = UILabel()
        label.text = "欢迎归来"
        return label
    }()
    
    
}
